<?php

class C5Sessions
{

    private $suffix_key = "C5_CLIENT";
    private $_SESSION_KEY_NAME = "_SESSION_PARAMETERS_KEY_C5_CLIENT";

    static public function get_global_session_matrix()
    {
        global $_SESSION;
        global $_SESSION_KEY_NAME;

        if (array_key_exists($_SESSION_KEY_NAME, $_SESSION)) {
            return unserialize($_SESSION[$_SESSION_KEY_NAME]);
        }
        return array();
    }

    static public function persist_global_session_matrix($matrix)
    {
        global $_SESSION;
        global $_SESSION_KEY_NAME;
        $_SESSION[$_SESSION_KEY_NAME] = serialize($matrix);
    }

    static public function request_to_session($name = "")
    {
        if (empty($name)) {
            $name = array_keys($_REQUEST);
        }
        if (is_array($name)) {
            for ($i = 0; $i < count($name); $i++) {
                // print $name[$i] ;
                self::request_to_session($name[$i]);
            }
            return;
        } else {
            if (isset($_REQUEST[$name])) {
                self::set_session($name, get($name));
            }
        }
    }

    static public function request_to_page_session($name)
    {
        global $_REQUEST;
        if (isset($_REQUEST[$name])) {
            self::set_page_session($name, get($name));
        }
    }

    static public function get_session($name, $defaultValue = "")
    {
        $matrix = self::get_global_session_matrix();
        if (array_key_exists($name, $matrix)) {
            return $matrix[$name];
        }
        return $defaultValue;
    }

    static public function get_sessions($names = [])
    {
        $matrix = [];
        foreach ($names as $name) {
            $matrix[$name] = self::get_session($name);
        }
        return $matrix;
    }

    static public function set_session($name, $object)
    {
        $matrix = self::get_global_session_matrix();
        $matrix[$name] = $object;
        self::persist_global_session_matrix($matrix);
    }

    static public function session_exists($name)
    {
        $defaultValue = "____aa____";
        return self::get_session($name, $defaultValue) != $defaultValue;
    }

    static public function remove_session($name)
    {
        $matrix = self::get_global_session_matrix();
        unset($matrix[$name]);
        self::persist_global_session_matrix($matrix);
    }

    static public function remove_all_session_parameters()
    {
        self::persist_global_session_matrix(array());
    }

    static public function get_page_parameter_session_key_name()
    {
        global $_SERVER;
        return str_replace("/", "_", $_SERVER["SCRIPT_NAME"]) . ":_PAGE_SESSION_";
    }

    static public function get_page_session_matrix()
    {
        $key = self::get_page_parameter_session_key_name();
        $matrix = self::get_global_session_matrix();
        if (array_key_exists($key, $matrix)) {
            return $matrix[$key];
        }
        $matrix = array();
        return $matrix;
    }

    static public function persist_page_session_matrix($matrix)
    {
        $key = self::get_page_parameter_session_key_name();
        $temp = self::get_global_session_matrix();
        $temp[$key] = $matrix;
        self::persist_global_session_matrix($temp);
    }

    static public function get_page_session($name, $defaultValue = "")
    {
        $matrix = self::get_page_session_matrix();
        if (array_key_exists($name, $matrix)) {
            return $matrix[$name];
        }
        return $defaultValue;
    }

    static public function get_session_parameters_with_prefix($prefix)
    {
        $matrix = self::get_global_session_matrix();
        return self::find_elements_with_key_prefix($matrix, $prefix);
    }

    static public function get_page_session_parameters_with_prefix($prefix)
    {
        $matrix = self::get_page_session_matrix();
        return self::find_elements_with_key_prefix($matrix, $prefix);
    }

    static public function find_elements_with_key_prefix($matrix, $prefix)
    {
        $result = [];
        foreach ($matrix as $key => $value) {
            if (substr($key, 1, strlen($value)) == $value) {
                $result[$key] = $value;
            }
        }
        return $result;
    }

    static public function set_page_session($name, $object)
    {
        $matrix = self::get_page_session_matrix();
        $matrix[$name] = $object;
        self::persist_page_session_matrix($matrix);
    }

    static public function remove_page_session($name)
    {
        $matrix = self::get_page_session_matrix();
        unset($matrix[$name]);
        self::persist_page_session_matrix($matrix);
    }


    static public function remove_all_page_session_parameters()
    {
        $matrix = self::get_global_session_matrix();
        $key = self::get_page_parameter_session_key_name();
        if (array_key_exists($key, $matrix)) {
            unset($matrix[$key]);
        }
        self::persist_global_session_matrix($matrix);
    }

    static public function deep_get($name, $isPageScope = false, $defaultValue = "", $type = "string")
    {
        $name = str_replace("[]", "", $name);
        $value = $defaultValue;
        if ($isPageScope) {
            self::request_to_page_session($name);
            $value = self::get_page_session($name, $defaultValue);
            // print_html($name."-------------".$value) ;
        } else {
            self::request_to_session($name);
            $value = self::get_session($name, $defaultValue);
        }
        if ("int" == $type) {
            $value = is_int($value * 1) ? $value : $defaultValue;
        }
        return $value;
    }


    static public function increment_session($key)
    {
        $num = self::get_session($key, 0);
        $num++;
        self::set_session($key, $num);
        return $num;
    }

}